package chapter05;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 10-1
 * 功能：lock.lock()的规范使用
 * <p>
 * 添加一个：lock.tryLock()的规范使用
 */
public class LockUseCase {
    public void lock() {
        Lock lock = new ReentrantLock();
        lock.lock();
        try {
        } finally {
            lock.unlock();
        }
    }

    public void tryLock() {
        Lock lock = new ReentrantLock();
        if (lock.tryLock()) { // 获取不到锁立刻返回，不加入"同步队列"
            try {
                // manipulate protected state
            } finally {
                lock.unlock();
            }
        } else {
            // perform alternative actions  }
        }
    }


}
